package com.world.business.user.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.world.business.user.domain.User;
import com.world.business.user.domain.query.UserAddRequest;
import com.world.business.user.domain.query.UserChangePasswordRequest;
import com.world.business.user.domain.query.UserPageRequest;
import com.world.business.user.domain.query.UserUpdateRequest;
import com.world.business.user.domain.vo.UserVO;
import com.world.business.user.service.IUserService;
import com.world.common.controller.BaseController;
import com.world.common.domain.PageResult;
import com.world.common.domain.ResponseData;
import com.world.common.domain.SuccessResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author 邓驴
 * @since 2023-05-13
 */
@Api(tags = "用户")
@RestController
@RequestMapping("/user")
public class UserController extends BaseController<User> {

    @Resource
    private IUserService userService;

    @ApiOperation("列表")
    @GetMapping("/list")
    public ResponseData<PageResult<UserVO>> list(UserPageRequest request) {
        Page<User> page = startPage(request);
        Page<User> result = userService.page(page);
        PageResult<UserVO> pageResult = getPageResult(result, UserVO.class);
        return new SuccessResponseData<>(pageResult);
    }

    @ApiOperation("详情")
    @GetMapping("/get/{id}")
    public ResponseData<UserVO> get(@PathVariable("id") Long id) {
        User user = userService.getById(id);
        if (BeanUtil.isEmpty(user)) {
            return new SuccessResponseData<>();
        }
        UserVO userVO = BeanUtil.copyProperties(user, UserVO.class);
        return new SuccessResponseData<>(userVO);
    }

    @ApiOperation("新增")
    @PostMapping("add")
    public ResponseData<Long> add(@RequestBody UserAddRequest request) {
        return new SuccessResponseData<>(userService.add(request));
    }

    @ApiOperation("修改")
    @PostMapping("/update")
    public ResponseData<Void> update(@RequestBody UserUpdateRequest request) {
        userService.update(request);
        return new SuccessResponseData<>();
    }

    @ApiOperation("重置密码")
    @GetMapping("/reset/pw")
    public ResponseData<Void> resetPassword(Long userId) {
        userService.resetPassword(userId);
        return new SuccessResponseData<>();
    }

    @ApiOperation("修改密码")
    @PutMapping("/change/pw")
    public ResponseData<Void> changePassword(@Validated @RequestBody UserChangePasswordRequest request) {
        userService.changePassword(request);
        return new SuccessResponseData<>();
    }

    @ApiOperation("初始化用户信息")
    @GetMapping("/init")
    public ResponseData<Void> initUser(Long userId) {
        userService.initUser(userId);
        return new SuccessResponseData<>();
    }


}
